<template>
  <div>
    <div class="login-wrap">
      <div class="ms-login">
        <div class="ms-title">购物车管理</div>
        <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
          <el-form-item prop="userName">
            <el-input v-model="param.userName" placeholder="userName">
              <template #prepend>
                <el-button :icon="User"></el-button>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="userPassword">
            <el-input
                type="password"
                placeholder="userPassword"
                v-model="param.userPassword"
                @keyup.enter="submitForm(login)"
            >
              <template #prepend>
                <el-button :icon="Lock"></el-button>
              </template>
            </el-input>
          </el-form-item>

          <div class="login-btn">
            <el-button type="primary" @click="submitForm(login)">登录</el-button>
          </div>
          <div>
            <el-button type="primary" @click="this.$router.push('Order')">订单</el-button>
          </div>
          <p class="login-tips">Tips : 用户名和密码随便填。</p>
        </el-form>
      </div>
    </div>
  </div>

</template>

<script setup lang="ts">
import {ref, reactive} from 'vue';
import {useRouter} from 'vue-router';
import {ElMessage} from 'element-plus';
import type {FormInstance, FormRules} from 'element-plus';
import {Lock, User} from '@element-plus/icons-vue';
import axios from "axios";


interface LoginInfo {
  userName: string;
  userPassword: string;
}

const router = useRouter();
const param = reactive<LoginInfo>({
  userName: '',
  userPassword: '',
});


const rules: FormRules = {
  userName: [
    {
      required: true,
      message: '请输入用户名',
      trigger: 'blur'
    }
  ],
  userPassword: [{required: true, message: '请输入密码', trigger: 'blur'}],
};

const login = ref<FormInstance>();
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid: boolean) => {
    if (valid) {
      axios.post('apa/user/login', {'userName': param.userName, 'passWord': param.userPassword}).then(p => {
        if (p.data !== '') {
          sessionStorage.setItem("user", p.data.userId)
          ElMessage({message: "登录成功", type: "success"})
          router.push('/index');
        } else {
          ElMessage.error('登录失败');
        }
      });
    } else {
      ElMessage.error('信息填写错误');
      return false;
    }
  });
};


</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 750px;
  /*background-image: url(../assets/img/login-bg.jpg);*/
  /*background-color: #2d8cf0;*/
  /* 背景渐变色 - 原理2 */
  /*background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);*/
  background: linear-gradient(-45deg, #FA8BFF, #2BD2FF, #2BFF88);
  /* 背景尺寸 - 原理3 */
  background-size: 600% 600%;
  /* 循环动画 - 原理4 */
  animation: gradientBG 7s ease infinite;

  /*background-size: 100%;*/


}

/* 动画，控制背景 background-position */
@keyframes gradientBG {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  border-bottom: 1px solid #ddd;
}

.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}

.ms-content {
  padding: 30px 30px;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}

.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #fff;
}
</style>
